﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using EasyFramework.Win32;

namespace EasyFramework.Utility
{
    /// <summary>
    /// 版权：广联达软件股份有限公司
    /// 创建：战东耀, 2012-04-11
    /// 说明: 运行时间统计工具类，来自《数据结构与算法 via C#》
    /// 修改: 
    /// </summary>
    public class TimeRunningUtil
    {
        TimeSpan startTime;
        TimeSpan duration;

        public TimeRunningUtil()
        {
            startTime = new TimeSpan(0);
            duration = new TimeSpan(0);
        }

        /// <summary>
        /// 代码运行前，开始记录时间
        /// </summary>
        public void StartTime()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            startTime = Kernel32Wrapper.GetProcessThreadByID().UserProcessorTime;
        }

        /// <summary>
        /// 代码运行后，终止记录时间
        /// </summary>
        public void StopTime()
        {
            duration = Kernel32Wrapper.GetProcessThreadByID().UserProcessorTime.Subtract(startTime);
        }

        /// <summary>
        /// 时间间隔结果
        /// </summary>
        public TimeSpan Result
        {
            get
            {
                return duration;
            }
        }
    }
}
